import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

class MyToggleButtonContainer extends MyComponentContainer {
    private JTextField buttonText;
    
    MyToggleButtonContainer(String name, MyFrame frame) {
        super(new JToggleButton(name), frame);
        ((JToggleButton)myComponent).setRolloverEnabled(false);
        ((JToggleButton)myComponent).addMouseListener(new MouseHandler());
        ((JToggleButton)myComponent).addMouseMotionListener(new MouseDragHandler());
    }
    
    MyToggleButtonContainer(Icon icon, MyFrame frame) {
        super(new JToggleButton(icon), frame);
        ((JToggleButton)myComponent).setRolloverEnabled(false);
        ((JToggleButton)myComponent).addMouseListener(new MouseHandler());
        ((JToggleButton)myComponent).addMouseMotionListener(new MouseDragHandler());
    }
    //#########################################################################
    public class MouseHandler extends MouseAdapter {
        public void mouseEntered(MouseEvent m) {
            if (isChosen()==true) {
                if (((JToggleButton)myComponent).getIcon()==null) {
                    frame.panel.setCursor(new Cursor(Cursor.TEXT_CURSOR));
                } else {
                    frame.panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
                }
            }
        }
        
        public void mouseExited(MouseEvent m) {
            frame.panel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
        
        public void mouseClicked(MouseEvent m) {
            if (m.getButton() == 3) {
                setChosen(true);
                showBorder();
                handleRightClickSelection();
                MyRightClickMenu popUpMenu = new MyRightClickMenu(MyToggleButtonContainer.this,m.getX(),m.getY(),frame);
            }
            if (m.getButton()==MouseEvent.BUTTON1) {
                if (m.getClickCount()==2) {
                    JComponent eventObject =(JComponent) m.getComponent(); /* Get the component that is
                     responsable for the action */
                    MyPropertiesWindow properties = new MyPropertiesWindow(MyToggleButtonContainer.this,frame);
                    /*Createing the properties window and send the object that is
                     resposable for the event*/
                }
                
                else if (m.getClickCount()==1 & m.isShiftDown()==false) {
                    if (isChosen()==false) {
                        if (((JToggleButton)myComponent).getIcon()==null) {
                            frame.panel.setCursor(new Cursor(Cursor.TEXT_CURSOR));
                        } else {
                            frame.panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
                        }
                        setChosen(true);
                        showBorder();
                    } else {
                        if (((JToggleButton)myComponent).getIcon()==null) {
                            hideBorder();
                            ((JToggleButton)myComponent).setVisible(false);
                            buttonText = new JTextField(((JToggleButton)myComponent).getText());
                            buttonText.setLocation(((JToggleButton)myComponent).getX(),((JToggleButton)myComponent).getY());
                            buttonText.setSize(((JToggleButton)myComponent).getWidth(),((JToggleButton)myComponent).getHeight());
                            buttonText.setHorizontalAlignment(JTextField.CENTER);
                            
                            buttonText.addFocusListener(new FocusAdapter() {public void focusLost(FocusEvent f) {
                                ((JToggleButton)myComponent).setText(buttonText.getText());
                                frame.panel.remove(buttonText);
                                ((JToggleButton)myComponent).setVisible(true);
                                frame.panel.repaint();
                                flagUndo=true;
                                handleUndo();
                            }
                            });
                            
                            buttonText.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent k) {
                                if (k.getKeyText(k.getKeyCode()).equals("Enter")) {
                                    ((JToggleButton)myComponent).setText(buttonText.getText());
                                    frame.panel.remove(buttonText);
                                    ((JToggleButton)myComponent).setVisible(true);
                                    showBorder();
                                    frame.panel.repaint();
                                }
                            }
                            });
                            
                            frame.panel.add(buttonText);
                            buttonText.grabFocus();
                        }
                    }
                }
            }
        }
        
        public void mousePressed(MouseEvent m) {
            frame.panel.requestFocusInWindow();
            
            if (m.getButton()==MouseEvent.BUTTON1) {
                handleSelection(m);
                frame.panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
                clickToAnchorX=m.getX();
                clickToAnchorY=m.getY();
            }
        }
        
        public void mouseReleased(MouseEvent m) {
            if (m.getButton()==MouseEvent.BUTTON1) {
                frame.panel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            }
            handleUndo();
        }
    }
    //#########################################################################
    class MouseDragHandler extends MouseMotionAdapter {
        public void mouseDragged(MouseEvent evt) {
            handleDrag(evt);
        }
    }
    //#########################################################################
}